package com.example.qincetest.utils

import android.graphics.Typeface
import android.text.SpannableString
import android.text.Spanned
import android.text.style.StyleSpan
import android.widget.TextView

object TextStyleUtils {
    /**
     * 设置TextView文本前半部分加粗
     * @param textView 目标TextView
     * @param text 完整文本内容
     * @param boldLength 需要加粗的长度，如果超过文本长度则全部加粗
     */
    fun setHalfBoldText(textView: TextView, text: String, boldLength: Int = text.length / 2) {
        val spannableString = SpannableString(text)
        val length = minOf(boldLength, text.length)
        spannableString.setSpan(
            StyleSpan(Typeface.BOLD),
            0,
            length,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        textView.paint.isFakeBoldText = true
        textView.text = spannableString
    }

    /**
     * 设置TextView指定范围文本加粗
     * @param textView 目标TextView
     * @param text 完整文本内容
     * @param start 加粗开始位置
     * @param end 加粗结束位置
     */
    fun setBoldText(textView: TextView, text: String, start: Int, end: Int) {
        val spannableString = SpannableString(text)
        if (start >= 0 && end <= text.length && start < end) {
            spannableString.setSpan(
                StyleSpan(Typeface.BOLD_ITALIC),
                start,
                end,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        }
        textView.text = spannableString
    }
}